<?php

namespace App\Http\Controllers\NewPay;

use App\Http\Controllers\Controller;
use App\Models\Pay\PayImportTemplate;
use App\Models\Pay\PayReport;
use App\Models\Pay\PayReportFields;
use Illuminate\Http\Request;

class PayReportController extends Controller
{

    public function getColumns(Request $request)
    {
        return response()->formatJson($this->getMenuList($request->all()));
    }

    public function getMenuList($params): array
    {
        $result = [];
        $parent = PayReportFields::query()
            ->where('report_id', $params['id'])
            ->where('parent_id', 0)
            ->orderBy('sort')
            ->get()
            ->toArray();

        foreach ($parent as $v) {
            $v['key'] = $v['id'];
            $v['expand'] = true;
            $v['title'] = $v['header_name'];
            $v['parentTitle'] = '一级项目';
            $v['is_show'] = (string) $v['is_show'];
            $son = PayReportFields::query()
                ->where('report_id', $params['id'])
                ->where('parent_id', $v['id'])
                ->orderBy('sort')
                ->get()
                ->toArray();
            $sonItem = [];
            foreach ($son as $key => $value) {
                $value['key'] = $value['id'];
                $value['expand'] = true;
                $value['title'] = $value['header_name'];
                $value['is_show'] = (string) $value['is_show'];
                $value['parentTitle'] = $v['header_name'];
                $value['tableNameCoolumn'] = [$value['table_name'], $value['column_name']];
                $sonItem[] = $value;
            }

            $v['children'] = $sonItem;
            $result[] = $v;
        }

        return $result;
    }

    public function initTableName(Request $request)
    {
        $data = [];
        $tableNames = PayImportTemplate::query()->get()->groupBy('table_name')->toArray();

        foreach ($tableNames as $title => $item) {
            $children = [];
            foreach ($item as $v) {
                $children[] = [
                    'label' => $v['column_name'],
                    'value' => $v['column_name'],
                ];
            }
            $data[] = [
                'label'    => $title,
                'value'    => $title,
                'isParent' => true,
                'children' => $children,
            ];
        }

        return response()->formatJson($data);
    }

    /**
     * 导入模板列表
     *
     * @param  Request  $request
     *
     * @return mixed
     */
    public function payReportList(Request $request)
    {
        return response()->formatJson(PayReport::query()->get()->toArray());
    }

    /**
     * 创建
     *
     * @param  Request  $request
     *
     * @return mixed
     */
    public function store(Request $request)
    {
        $params = $request->all();

        $report = new PayReport($params);
        $result = $report->save();

        $message = $result ? '添加成功' : '添加失败';

        return response()->formatJson($result, $message);
    }

    /**
     * 修改模板
     *
     * @param  Request  $request
     * @param $id
     *
     * @return mixed
     */
    public function update(Request $request, $id)
    {
        $params = $request->all();

        $payReport = PayReport::query()->find($id);
        $result = $payReport->update(['report_name' => $params['report_name']]);

        $message = $result ? '添加成功' : '添加失败';


        return response()->formatJson($result, $message);
    }

    /**
     * 删除车辆信息
     *
     * @param  Request  $request
     *
     * @return mixed
     */
    public function destroy(Request $request)
    {
        $result = false;
        if ($request->has('id')) {
            $delCount = PayReport::destroy($request->input('id'));
            $result = $delCount !== 0;
        }

        $message = $result ? '删除成功' : '删除失败';

        return response()->formatJson($result, $message);
    }

    public function addRoot(Request $request)
    {
        $params = $request->all();

        if (array_key_exists('tableNameCoolumn', $params) && $params['tableNameCoolumn']) {
            $params['table_name'] = $params['tableNameCoolumn'][0];
            $params['column_name'] = $params['tableNameCoolumn'][1];
        }
        $report = new PayReportFields($params);
        $result = $report->save();

        $message = $result ? '添加成功' : '添加失败';

        return response()->formatJson($result, $message);
    }

    public function updateRoot(Request $request)
    {
        $params = $request->all();

        if (array_key_exists('tableNameCoolumn', $params)) {
            if ($params['tableNameCoolumn']) {
                $params['table_name'] = $params['tableNameCoolumn'][0];
                $params['column_name'] = $params['tableNameCoolumn'][1];
            } else {
                $params['table_name'] = $params['column_name'] = '';
            }
        }
        $payReport = PayReportFields::query()->find($params['id']);
        $result = $payReport->update($params);

        $message = $result ? '修改成功' : '修改失败';

        return response()->formatJson($result, $message);
    }
}
